﻿using System.Windows.Markup;
using System.IO;

namespace InnovTech.Wpf.Util
{
    public static class XamlUtil
    {
        /// <summary>
        /// get a object from a xaml file by the given file path
        /// </summary>
        /// <param name="filePath">the file path where the xaml file stands</param>
        /// <returns>a object which is contained in the xaml file in the given file path</returns>
        public static object GetObjectFromXamlFile(string filePath)
        {
            using (FileStream stream = new FileStream(filePath, FileMode.Open))
            {
                return XamlReader.Load(stream);
            }
        }

        /// <summary>
        /// get a object the given string which contains the content a xaml file
        /// </summary>
        /// <param name="content">the content a xaml file</param>
        /// <returns>a object which is contained in the given xaml string</returns>
        public static object GetObjectFromXamlContentString(string content)
        {
            int index = content.IndexOf('<');
            return index < 0 ? null : XamlReader.Parse(content.Substring(index));
        }
    }
}
